home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / errlvl12.zip / FINDELVL.C < prev    next >
C/C++ Source or Header  |  1994-03-08  |  5KB  |  93 lines

  1. /*-------------------------------------------------------------------------*/
  2. /* Program:    FindElvl.C                                                  */
  3. /* Purpose:    Aids in finding the location of ERRORLEVEL within           */
  4. /*                COMMAND.COM's PSP.                                       */
  5. /* Notes:      Compiles with Borland C++, v3.1. Should work on any         */
  6. /*                machine running MS-DOS, v2.xx or higher.                 */
  7. /*             What this program does is scan thru the PSP owned by        */
  8. /*                COMMAND.COM for a value -- the *known* return code from  */
  9. /*                the previous program -- specified by the user. Offsets   */
  10. /*                with matches are printed. You'll need to iterate at      */
  11. /*                least twice to cull out spurious matches. In practice,   */
  12. /*                I've noticed 145, 147, 148, 155, 159, and 167 provide    */
  13. /*                good guesses; ie, few matches.                           */
  14. /*             You should disregard output from the first run unless you   */
  15. /*                know exactly what the value of ERRORLEVEL already is.    */
  16. /*             If you have a copy of UNIQ, the following commandline       */
  17. /*                should do the trick: "findelvl | findelvl 145 147 |      */
  18. /*                findelvl 147 148 | sort | uniq -d".                      */
  19. /* Status:     Released into the public domain. Enjoy! If you use it,      */
  20. /*                let me know what you think. You don't have to send       */
  21. /*                any money, just comments and suggestions.                */
  22. /* Updates:    28-Dec-90, GAT                                              */
  23. /*                - initial version.                                       */
  24. /*             03-Jul-93, GAT                                              */
  25. /*                - minor changes in comments.                             */
  26. /*-------------------------------------------------------------------------*/
  27.  
  28. /*-------------------------------------------------------------------------*/
  29. /* Author:     George A. Theall                                            */
  30. /* SnailMail:  TifaWARE                                                    */
  31. /*             610 South 48th St                                           */
  32. /*             Philadelphia, PA.  19143                                    */
  33. /*             U.S.A.                                                      */
  34. /* E-Mail:     george@tifaware.com                                         */
  35. /*             theall@popmail.tju.edu                                      */
  36. /*             theall@mcneil.sas.upenn.edu                                 */
  37. /*             george.theall@satalink.com                                  */
  38. /*-------------------------------------------------------------------------*/
  39.  
  40. #include <stdio.h>
  41. #include <dos.h>                             /* for MK_FP(), _psp, etc... */
  42. #include <io.h>                              /* for isatty() */
  43. #include <stdlib.h>                          /* for exit() */
  44.  
  45.  
  46. int main(int argc, char *argv[])
  47. {
  48.  
  49.    char ch;
  50.    unsigned char oldrc,                      /* value to scan for */
  51.       newrc;                                 /* return code to use */
  52.    unsigned int aPSP, tempPSP;               /* values for PSPs */
  53.    unsigned int ofs;                         /* offset with a segment */
  54.  
  55.    /* Parse arguments and provide syntax if necessary. */
  56.    if (argc != 3) {
  57.       puts("usage: findelvl oldrc newrc\nnow exiting with rc=145");
  58.       return 145;
  59.    }
  60.    oldrc = atoi(argv[1]);
  61.    newrc = atoi(argv[2]);
  62.  
  63.    /* Find PSP for COMMAND.COM. */
  64.    aPSP = _psp;
  65.    do {
  66.      tempPSP = aPSP;
  67.      aPSP = *((unsigned int far *) MK_FP(aPSP, 0x16));
  68.    } while (aPSP < tempPSP);
  69.    if (aPSP > tempPSP) {
  70.       puts("findelvl: can't locate PSP for COMMAND.COM");
  71.       return 255;
  72.    }
  73.  
  74.    /*
  75.     * Pass anything in stdin straight thru. This lets the user combine
  76.     * output from earlier runs with uniq to quickly identify duplicates.
  77.     */
  78.    if (isatty(fileno(stdin)) == 0)
  79.       while ((ch = getchar()) != EOF)
  80.          putchar(ch);
  81.  
  82.    /* Scan through segment reporting matches. */
  83.    printf("DOS v%u.%u; PSP at segment: %#06x\n", _osmajor, _osminor, aPSP);
  84.    printf("Matches for rc=%u at offsets:\n", oldrc);
  85.    for (ofs = 0; ofs < 0xffff; ++ofs)
  86.       if (oldrc == *(unsigned char far *) (MK_FP(aPSP, ofs)))
  87.          printf("  %#06x\n", ofs);
  88.    if (oldrc == *(unsigned char far *) (MK_FP(aPSP, ofs)))  /* segment end */
  89.       printf("  %#06x\n", ofs);
  90.  
  91.    return newrc;
  92. }
  93.